package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;

import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.Leisure;
import com.dsteiner.xelavos.client.model.CreateObjectModel;
import com.google.appengine.api.datastore.Text;

public class CreateLeisureController implements IModelObjectBuilder{

	private PrefilledTextBoxModel descriptionTextBoxModel;
	private PrefilledTextBoxModel eventDateTextBoxModel;
	private PrefilledTextBoxModel nameTextBoxModel;
	private PrefilledTextBoxModel placeTextBoxModel;
	private PrefilledTextBoxModel eventHourTextBoxModel;
	private PrefilledTextBoxModel eventDateUntilTextBoxModel;
	private PrefilledTextBoxModel contactTextBoxModel;
	private CreateObjectModel createObjectModel;
	private IJsUtil jsUtil;

	public ModelObject create() {
		String name = nameTextBoxModel.getText();
		String place = placeTextBoxModel.getText();
		String description = descriptionTextBoxModel.getText();
		String eventDate = eventDateTextBoxModel.isEdited() ? eventDateTextBoxModel.getText() : null;
		String eventDateUntil = eventDateUntilTextBoxModel.isEdited() ? eventDateUntilTextBoxModel.getText() : null;
		String eventHour = eventHourTextBoxModel.isEdited() ? eventHourTextBoxModel.getText() : null;
		String contact = contactTextBoxModel.isEdited() ? contactTextBoxModel.getText() : null;
		Date eventDateAsDate = DateUtilImpl.parseDate(eventDate, DateUtilImpl.formatterSpanish); 
		Date eventDateUntilAsDate = DateUtilImpl.parseDate(eventDateUntil, DateUtilImpl.formatterSpanish); 
		Date eventHourAsDate = DateUtilImpl.parseDate(eventHour, DateUtilImpl.formatterHour);
		nameTextBoxModel.setHasError(false);
		placeTextBoxModel.setHasError(false);
		descriptionTextBoxModel.setHasError(false);
		eventDateTextBoxModel.setHasError(false);
		eventHourTextBoxModel.setHasError(false);
		eventDateUntilTextBoxModel.setHasError(false);
		
		StringBuffer text = new StringBuffer();
		if(!nameTextBoxModel.isEdited()){
			nameTextBoxModel.setHasError(true);
			text.append("ingrese un nombre\n");
		}
		if(!placeTextBoxModel.isEdited()){
			placeTextBoxModel.setHasError(true);
			text.append("ingrese una lugar\n");
		}
		if(!descriptionTextBoxModel.isEdited()){
			text.append("ingrese una descripcion\n");
			descriptionTextBoxModel.setHasError(true);
		}
		if(eventDate != null && eventDateAsDate == null){
			text.append("ingrese la fecha en el formato dd/mm/aaaa\n");
			eventDateTextBoxModel.setHasError(true);
		}
		if(eventDateUntil != null && eventDateUntilAsDate == null){
			text.append("ingrese la fecha en el formato dd/mm/aaaa\n");
			eventDateUntilTextBoxModel.setHasError(true);
		}
		if(eventDateUntilAsDate != null && eventDate == null){
			text.append("ingrese la fecha inicio");
			eventDateTextBoxModel.setHasError(true);
		}
		if(eventHour != null && eventHourAsDate == null){
			text.append("ingrese la fecha en el formato hh:mm\n");
			eventHourTextBoxModel.setHasError(true);
		}
		
		if(text.length() > 0){
			jsUtil.trackEvent(Constants.CAT_PUBLISHING, Constants.ACT_MSG_SHOWN, text.toString());
			JsUtil.getInstance().showAlert(text.toString());
			return null;
		}

		Leisure leisure = new Leisure();
		if(createObjectModel.getCreateModelObject() != null)
			leisure = (Leisure) createObjectModel.getCreateModelObject();
		leisure.setName(name);
		leisure.setPlace(place);
		leisure.setDescription(description != null ? new Text(description) : null);
		leisure.setRegisteredDate(new Date());
		leisure.setEventDate(eventDate);
		leisure.setEventDateUntil(eventDateUntil);
		leisure.setEventHour(eventHour);
		leisure.setContact(contact);
		leisure.setEventDateAsDate(eventDateAsDate);
		leisure.setEventDateUntilAsDate(eventDateUntilAsDate);
		
		return leisure;
	}

	public void registerContactTextBoxModel(PrefilledTextBoxModel contactTextBoxModel) {
		this.contactTextBoxModel = contactTextBoxModel;
	}

	public void registerCreateObjectModel(CreateObjectModel createObjectModel) {
		this.createObjectModel = createObjectModel;
		createObjectModel.addPropertyChangeListener("createModelObject", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateUi();
			}
		});
	}

	public void registerCreateObjectWidget(ILazyWidget createObjectWidget) {
		createObjectWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateUi();
			}
		});
	}
	
	public void registerDescriptionTextBoxModel(PrefilledTextBoxModel descriptionTextBoxModel) {
		this.descriptionTextBoxModel = descriptionTextBoxModel;
	}
	
	public void registerEventDateTextBoxModel(PrefilledTextBoxModel eventDateTextBoxModel) {
		this.eventDateTextBoxModel = eventDateTextBoxModel;
	}
	
	public void registerEventDateUntilTextBoxModel(PrefilledTextBoxModel eventDateUntilTextBoxModel) {
		this.eventDateUntilTextBoxModel = eventDateUntilTextBoxModel;
	}

	public void registerEventHourTextBoxModel(PrefilledTextBoxModel eventHourTextBoxModel) {
		this.eventHourTextBoxModel = eventHourTextBoxModel;
	}

	public void registerNameTextBoxModel(final PrefilledTextBoxModel nameTextBoxModel) {
		this.nameTextBoxModel = nameTextBoxModel;
		nameTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!nameTextBoxModel.hasFocus())
					updateImageSearch();
			}
		});
	}
	
	public void registerPlaceTextBoxModel(PrefilledTextBoxModel placeTextBoxModel) {
		this.placeTextBoxModel = placeTextBoxModel;
	}

	private void updateImageSearch() {
		StringBuffer imageSearch = new StringBuffer("");

		if(nameTextBoxModel.isEdited())
			imageSearch.append(nameTextBoxModel.getText()).append(" ");
		
		createObjectModel.setImageSearch(imageSearch.length() > 0 ? imageSearch.toString() : null);
	}

	private void updateUi() {
		ModelObject object = createObjectModel.getCreateModelObject();
		if(object != null && !(object instanceof Leisure))
			return;
		Leisure createModelObject = (Leisure) createObjectModel.getCreateModelObject();
		descriptionTextBoxModel.setText(createModelObject != null ? createModelObject.getDescription() != null ? createModelObject.getDescription().toString() : null : null);
		eventDateTextBoxModel.setText(createModelObject != null ? createModelObject.getEventDate() : null);
		eventDateUntilTextBoxModel.setText(createModelObject != null ? createModelObject.getEventDateUntil() : null);
		nameTextBoxModel.setText(createModelObject != null ? createModelObject.getName() : null);
		placeTextBoxModel.setText(createModelObject != null ? createModelObject.getPlace() : null);
		eventHourTextBoxModel.setText(createModelObject != null ? createModelObject.getEventHour() : null);
		contactTextBoxModel.setText(createModelObject != null ? createModelObject.getContact() : null);
		updateImageSearch();
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
	}
	
}